package edu.kit.kitcampusguide.poimanager.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.HttpMethodConstraint;
import javax.servlet.annotation.ServletSecurity;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.kit.kitcampusguide.poimanager.persistence.POIGateway;
import edu.kit.kitcampusguide.poimanager.model.POI;

/**
 * 
 * 
 * @author Robert Reutter
 * 
 *         The Annotation is used to declare the URL where the servlet can be
 *         found. This replaces the declaration in the web.xml descriptor file
 */
@WebServlet(description = "", urlPatterns = { "/protected/manager" })
//@ServletSecurity(httpMethodConstraints = {@HttpMethodConstraint (value= "POST", rolesAllowed = "member")})
public class ManagementServlet extends HttpServlet {

	private static final String POI_PARAM_ID = "poiId";
	private static final String SELECTED_POI = "poi";
	private static final String MANAGEMENT_PAGE = "management.jsp";
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ManagementServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException 
	{
		
		//getting poiId param, parsing to integer
		int poiId = Integer.parseInt(request.getParameter(POI_PARAM_ID));
	
		POI selectedPOI = POIGateway.getInstance().readPOI(poiId);
		
		//put selectedPoi to session attributes
		request.getSession().setAttribute(SELECTED_POI, selectedPOI);
		response.sendRedirect(MANAGEMENT_PAGE);
		
	} // - doGet
	
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException 
	{
		if(request.getParameter("create")!=null)
		{
			//create a new POI with the Form-Data
			
			int id = Integer.parseInt(request.getParameter("ID"));
			String name = request.getParameter("Name");
			String category = request.getParameter("Category");
			Double latitude = Double.parseDouble(request.getParameter("Latitude"));
			Double longitude = Double.parseDouble(request.getParameter("Longitude"));
			
			//save the new POI in the database 
			POIGateway.getInstance().createPOI(id, name, category, latitude, longitude);
			//set the new POI as Selected POI
			request.getSession().setAttribute(SELECTED_POI, POIGateway.getInstance().readPOI(id));
			response.sendRedirect(MANAGEMENT_PAGE);
		}
		if(request.getParameter("delete")!=null)
		{
			//delete POI with the ID from the Management-Form
			POIGateway.getInstance().deletePOI(Integer.parseInt(request.getParameter("ID")));
			response.sendRedirect(MANAGEMENT_PAGE);
		}
		if(request.getParameter("update")!=null)
		{
			//Update POI in the Database
			POIGateway.getInstance().updatePOI(Integer.parseInt(request.getParameter("ID")) , request.getParameter("Name") , 
					request.getParameter("Category") , Double.parseDouble(request.getParameter("Latitude")) , 
					Double.parseDouble(request.getParameter("Longitude")) );
			request.getSession().setAttribute(SELECTED_POI, POIGateway.getInstance().readPOI(Integer.parseInt(request.getParameter("ID"))));
			response.sendRedirect(MANAGEMENT_PAGE);
		}
		
	} // - doPost 

} // - class
